//
//  ST_OpenBlindBoxBrowser.swift
//  btvideo
//
//  Created by Jason on 2021/11/9.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import NSObject_Rx
import SVGAPlayer
//import Rswift

enum ST_DrawOutBlindBox{
    case svga(url:URL?)
    case normal(str:String)
}

class ST_OpenBlindBoxBrowser: UIViewController {

    let giftName:String
    let box:ST_DrawOutBlindBox
    
    
    init(box:ST_DrawOutBlindBox,giftName:String){
        self.giftName = giftName
        self.box = box
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear
        
        view.addSubview(animateView)
        
        animateView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(ScreenWidth)
        }
        
        animateView.play {[weak self] _ in
            self?.nextStep()
        }
        
    }
    
    private func nextStep(){
        
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(81-20+statusBarHeight)
            make.height.equalTo(36)
        }
        
        switch box {
        case .normal(let str):
            playNormal(urlStr: str)
        case .svga(let url):
            play_svga_file(svgaUrl: url)
        }
    }
    
    private func playNormal(urlStr:String){
        
        self.view.addSubview(scan_animate_view)
        scan_animate_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(150.0/375.0*ScreenWidth)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(iPhoneXSeries ? -80 : -10)
        }
        
        self.view.addSubview(scan_gift_image_view)
        scan_gift_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(116/375.0*ScreenWidth)
            make.center.equalTo(scan_animate_view)
        }
        
        scan_gift_image_view.pin_setImage(from: urlStr.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        animateView.removeFromSuperview()
        
        
        scan_animate_view.play()
        
        main_thread_execute(afterDelay: 2) {
            self.goback_previous_controller(animated: false)
        }
        
    }
    
    
    private func play_svga_file(svgaUrl:URL?){
    
        view.addSubview(player)
        player.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        animateView.removeFromSuperview()
        
        
        ST_GiftAssetProvider.playSVGASession(urlString: svgaUrl?.absoluteString).withUnretained(self).subscribe { own , entity in
        
            own.player.videoItem = entity
            
            own.player.startAnimation()
            
        } onError: {[weak self] e in
            
            self?.goback_previous_controller()
            
        }.disposed(by: rx.disposeBag)
        
    }
    

    // MARK: - properties
     
    fileprivate lazy var titleLabel:ST_OpenBlindBoxTitleLabel = {
        let v = ST_OpenBlindBoxTitleLabel(title: "恭喜拆出 - \(giftName)")
        return v
    }()
    
     fileprivate lazy var animateView:LOTAnimationView = {
         
         let path = Bundle.main.path(forResource: "open_blind_box", ofType: "json")!
         
         let control = LOTAnimationView.init(filePath: path)
         
         control.contentMode = UIView.ContentMode.scaleAspectFill
         
         return control
     }()
    
    
    fileprivate lazy var player:SVGAPlayer = {
        
        let p  = SVGAPlayer()
        p.delegate = self
        p.loops = 1
        return p
    }()
    
    
    fileprivate lazy var scan_gift_image_view:UIImageView = {
       
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 116/2.0
        return control
    }()
    
    fileprivate lazy var scan_animate_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "receiveGiftBack", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
        control.loopAnimation = true
        
        return control
    }()
}




extension ST_OpenBlindBoxBrowser:SVGAPlayerDelegate{
    
    
    func svgaPlayerDidFinishedAnimation(_ player: SVGAPlayer!) {
        
        self.goback_previous_controller()
        
    }
    
}


class ST_OpenBlindBoxTitleLabel: UIView {
    
    init(title:String){
        
        super.init(frame: .zero)
        
        titleLabel.text = title
        
        addSubviews([imageView,titleLabel])
        
        imageView.snp.makeConstraints { make in
            make.width.height.equalTo(35)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(5)
        }
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(43)
        }
        
        self.backgroundColor =  .black
        self.cornerRadius = 8
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var titleLabel:UILabel = {
        let l = UILabel()
        l.textColor = .white
        l.font = .systemFont(ofSize: 14, weight: .medium)
        return l
    }()
    
    fileprivate lazy var imageView:UIImageView = {
        let v = UIImageView(image: ST_ImageUtils.imageName("blind_box_small_icon"))
        v.contentMode = .scaleAspectFill
        v.clipsToBounds = true
        //v.cornerRadius = 5
        return v
    }()
    
}

